﻿
var CP_IsCandSet = false;
var CP_ApplicationsCount = 0;
var CP_HeardAbout = '';
var CP_HeardAboutNotes = '';
var CP_CandidateID = -1;
var CP_CandidateName = '';
var CP_CandidateEmail = '';
var CP_CandidatePhone = '';
var CP_HeardAboutID = -1;

var DOWNLOAD_PAGE_URL = '';

$(function () {
    $("#tabs").tabs();

    $(".DocumentsList").menu();
    $(".DocumentsList").find("li").dblclick(OnDocumentDoubleClick);
    $(".BtnDownloadFile").button({
        icons: { primary: 'ui-icon-circle-arrow-s' },
        text: false
    });
    $(".DocumentsList").find("li").click(OnDocumentSingleClick);

    $("#AddNewApplicationButton").button();
    $("#AddNewApplicationButton").click(function () {
        ApplicationDlg_CurrentCandidateID = CP_CandidateID;
        return ShowPopup('#AddApplicationPopup');
    });

    $("#EditCandidate").button();
    $("#EditCandidate").click(function () {
        ShowEditDialog();
        return false;
    });

    $(".BtnUploadFile").button();

    //make the position and states box a jquery ui combobox
    $(".App_Position").combobox({
        valueselect: OnPositionSelected,
        source: []
    });
    $(".App_State").combobox({
        valueselect: OnStateSelected,
        source: []
    });

    SetPositionAndStateData();

    //make the position and state a true combo box, not editable
    $(".App_Combobox").find("input").attr('readonly', 'readonly');

    if (!CP_IsCandSet) {
        //hide the tabs
        $("#tabs").addClass("ui-helper-hidden");
        //hide cand info
        $("#CandidateInfo").addClass("ui-helper-hidden");

        //hide the buttons
        $("#ButtonHolder").addClass("ui-helper-hidden");

        //show the error msg
        $("#NoCandidateMsg").removeClass("ui-helper-hidden");
    }
    else if (CP_ApplicationsCount == 0) {
        //hide the tabs
        $("#tabs").addClass("ui-helper-hidden");

        //show the error msg
        $("#NoApplicationsMsg").removeClass("ui-helper-hidden");
    }

    if (CP_HeardAboutNotes.length > 0) {
        $("#CandidateInfoNotesIcon").removeClass("ui-helper-hidden");
    }

});

function UploadFileForApp(appID){
    UploadDlg_AppID = appID;
    return ShowPopup('#UploadDocumentPopup');
}

function OnFileUploadDoneOverride(data) {
    if (data.success) {
        $('#UploadDocumentPopup').dialog("close");
        //alert(data.category);
    }
    else {
        //TODO show error
        alert('error uploading');
    }
}

function OnStateSelected(event, ui) {
    var stateID = ui.item.itemID;
    var appID = $(this).siblings(".App_ID").first().val();

    PageMethods.ChangeApplicationState(appID, stateID,
        function () { },
        function () { /*TODO: show error*/ }
    );
}

function OnPositionSelected(event, ui) {
    var posID = ui.item.itemID;
    var appID = $(this).siblings(".App_ID").first().val();

    PageMethods.ChangeApplicationPosition(appID, posID,
        function () { },
        function () { /*TODO: show error*/ }
    );
}

function SetPositionAndStateData() {
    PageMethods.GetAddAppDlgData_Json(
        function (json) {

            var positions = eval(json)[1];
            var states = eval(json)[2];

            // Send the data to the Positions Combobox
            $(".App_Position").combobox("option", "source", positions);
            // Send the data to the State Combobox
            $(".App_State").combobox("option", "source", states);

            //now set each combo box based on the current values

            $(".App_Position").each(function (i) {
                var posName = '';
                for (var j in positions) {
                    if (positions[j].itemID == $(this).siblings(".App_Position_ID").first().val()) {
                        posName = positions[j].value;
                    }
                }
                $(this).siblings("input").first().val(posName);
            });

            $(".App_State").each(function (i) {
                var stateName = '';
                for (var j in states) {
                    if (states[j].itemID == $(this).siblings(".App_State_ID").first().val()) {
                        stateName = states[j].value;
                    }
                }
                $(this).siblings("input").first().val(stateName);
            });

        },
        function () {
            // TODO: SHOW ERROR MESSAGE
            $(".App_Combobox").addClass("ui-state-error");

            //set empty data
            $(".App_Position, .App_State").combobox("option", "source", []);
        }
    );
}

var old_dlg_title;
var old_dlg_buttons;
var old_dlg_data;

function ShowEditDialog(){

    //title
    //buttons
    //data for cand searcher

    //save dialog data
    old_dlg_title = $("#AddCandidatePopup").dialog("option", "title");
    old_dlg_buttons = $("#AddCandidatePopup").dialog("option", "buttons");
    old_dlg_data = $("#CandPopup_Name").autocomplete("option", "source");

    //set dlg data for our specific needs
    $("#AddCandidatePopup").dialog("option", "title", "Edit Candidate");
    $("#AddCandidatePopup").dialog("option", "buttons", 
        [
            {
                text: "Save",
                click: OnEditCandidateSaved
            },
            {
                text: "Cancel",
                click: function () { $(this).dialog("close"); }
            }
        ]
    );
    //we don't want this to be an autocomplete when we are just editing
    $("#CandPopup_Name").autocomplete("option", "source", []);

    $("#AddCandidatePopup").bind("dialogclose", EditCandDlgClose);

    $("#AddCandidatePopup").dialog("open");

    //set our data from our candidate
    $("#CandPopup_Name").val(CP_CandidateName);
    $("#CandPopup_Email").val(CP_CandidateEmail);
    $("#CandPopup_Phone").val(CP_CandidatePhone);
    $("#CandPopup_Combobox").find("input").first().val(CP_HeardAbout);
    $("#CandPopup_Notes").val(CP_HeardAboutNotes);
}

function EditCandDlgClose(){
    $("#AddCandidatePopup").dialog("option", "title", old_dlg_title);
    $("#AddCandidatePopup").dialog("option", "buttons", old_dlg_buttons);
    $("#CandPopup_Name").autocomplete("option", "source", old_dlg_data);

    $("#AddCandidatePopup").unbind("dialogclose", EditCandDlgClose);
}

function OnEditCandidateSaved(){
    //clear out any messages
    $("#CandPopup_Message").addClass("ui-helper-hidden");
    $("#CandPopup_Error").addClass("ui-helper-hidden");

    //get all the data
    var name = $("#CandPopup_Name");
    var email = $("#CandPopup_Email");
    var phone = $("#CandPopup_Phone");
    var heard = $("#CandPopup_Combobox").find("input").first();
    var heardContainer = $("#CandPopup_HeardAbout");
    var notes = $("#CandPopup_Notes");

    //validate the data
    var bValid = true;

    //name can't be empty
    if (name.val().length == 0) {
        name.addClass("ui-state-error");
        bValid = false;
    }
    else {
        name.removeClass("ui-state-error");
    }

    //heard about can't be empty
    if (CandidateDlg_HeardAboutID == -1) {
        CandidateDlg_HeardAboutID = CP_HeardAboutID;
    }

    //TODO: warn if they are adding a new heard about to database

    if (bValid) {
        //valid, so add the candidate and close the dialog

        //call page methods to edit candidate
        PageMethods.EditCandidate(CP_CandidateID, name.val(), email.val(), phone.val(), CandidateDlg_HeardAboutID, notes.val(),
            function () {
                //TODO Show success msgg???

                //reflect the changes on the page
                UpdateValuesOnPage(name.val(), phone.val(), email.val(), CandidateDlg_HeardAboutID, heard.val(), notes.val());

                //close the dlg
                $("#AddCandidatePopup").dialog("close");
            },
            function () {
                //error, show message and don't close dlg
                $("#CandPopup_Error").removeClass("ui-helper-hidden");
            }
        );
    }
    else {
        //not valid, show message
        $("#CandPopup_Message").removeClass("ui-helper-hidden");
    }
}

function UpdateValuesOnPage(newName, newPhone, newEmail, newHeardID, newHeardName, newNotes) {
    CP_CandidateName = newName;
    CP_CandidatePhone = newPhone;
    CP_CandidateEmail = newEmail;
    CP_HeardAboutID = newHeardID;
    CP_HeardAbout = newHeardName;
    CP_HeardAboutNotes = newNotes;

    //now set the values in the actual html elements
    $("#CandidateInfoName").html(newName);
    $("#CandidateInfoPhone").html(newPhone);
    $("#CandidateInfoHeardAbout").html(newHeardName);
    $("#CandidateInfoNotesIcon").attr("title", newNotes);
    $("#CandidateInfoEmail").find("a").attr("href", "mailto:" + newEmail).html(newEmail);

    if (newNotes.length > 0) {
        $("#CandidateInfoNotesIcon").removeClass("ui-helper-hidden");
    }
    else {
        $("#CandidateInfoNotesIcon").addClass("ui-helper-hidden");
    }
}

function OnDocumentSingleClick() { 

}

function OnDownloadDocumentClick(id) {
    window.open(DOWNLOAD_PAGE_URL + '?docID=' + id, 'Download');
}

function OnDocumentDoubleClick() {
}